package org.study.redis.api.util.jedis;

import redis.clients.jedis.Jedis;

import java.util.List;

/**
 * Redis 操作工具类：ScriptingKey
 * 参考 {@link redis.clients.jedis.commands.ScriptingKeyCommands}
 *
 * @author doveylovey
 * @email 1135782208@qq.com
 * @date 2022年04月23日
 */
public class RedisScriptingKeyUtils {
    private RedisScriptingKeyUtils() {
        throw new AssertionError("不能实例化 RedisScriptingKeyUtils 工具类");
    }

    public static Object eval(String script) {
        Object eval;
        try (Jedis jedis = SysJedisPool.getResource()) {
            eval = jedis.eval(script);
        }
        return eval;
    }

    public static Object eval(String script, int keyCount, String... params) {
        Object eval;
        try (Jedis jedis = SysJedisPool.getResource()) {
            eval = jedis.eval(script, keyCount, params);
        }
        return eval;
    }

    public static Object eval(String script, List<String> keys, List<String> args) {
        Object eval;
        try (Jedis jedis = SysJedisPool.getResource()) {
            eval = jedis.eval(script, keys, args);
        }
        return eval;
    }

    public static Object evalsha(String sha1) {
        Object evalsha;
        try (Jedis jedis = SysJedisPool.getResource()) {
            evalsha = jedis.evalsha(sha1);
        }
        return evalsha;
    }

    public static Object evalsha(String sha1, int keyCount, String... params) {
        Object evalsha;
        try (Jedis jedis = SysJedisPool.getResource()) {
            evalsha = jedis.evalsha(sha1, keyCount, params);
        }
        return evalsha;
    }

    public static Object evalsha(String sha1, List<String> keys, List<String> args) {
        Object evalsha;
        try (Jedis jedis = SysJedisPool.getResource()) {
            evalsha = jedis.evalsha(sha1, keys, args);
        }
        return evalsha;
    }

    /*public static void main(String[] args) {
        System.out.println("eval：" + eval("return redis.call('set', 'foo', 'bar')"));
        System.out.println("eval：" + eval("return redis.call('set', KEYS[1], 'bar')", 1, "foo"));
        System.out.println("eval：" + eval("return {KEYS[1], KEYS[2], ARGV[1], ARGV[2]}", Lists.newArrayList("key1", "key2"), Lists.newArrayList("first", "second")));

        System.out.println("evalsha：" + evalsha("return redis.call('set', 'foo', 'bar')"));
        System.out.println("evalsha：" + evalsha("return redis.call('set', KEYS[1], 'bar')", 1, "foo"));
        System.out.println("evalsha：" + evalsha("return {KEYS[1], KEYS[2], ARGV[1], ARGV[2]}", Lists.newArrayList("key1", "key2"), Lists.newArrayList("first", "second")));
    }*/
}
